package com.qijian.tool.controller;

import cn.hutool.json.JSONObject;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.redis.RedisCache;
import com.qijian.common.utils.DateUtils;
import com.qijian.tool.service.IReportService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(tags = "所有报表关联数据")
@RestController
@RequiredArgsConstructor
@RequestMapping("/report")
public class ReportController {

    private final IReportService reportService;
    private final RedisCache redisCache;

    @GetMapping("/app/index")
    public AjaxResult projectCountRpt() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("sales", reportService.salesSum());
        jsonObject.set("project", reportService.projectCount());
        jsonObject.set("quality", reportService.qualitySum());
        jsonObject.set("benefit", reportService.benefitSum());
        jsonObject.set("consumption", reportService.consumptionSum());
        Map<String, Object> map = DateUtils.calEvalTime(DateUtils.getDate(), redisCache.getCacheObject("sys_config:eval.time.range"));
        jsonObject.set("eval", reportService.evalAvg((String) map.get("beginTime"), (String) map.get("endTime")));
        return AjaxResult.success(jsonObject);
    }

    @GetMapping("/app/benefit/detail")
    public AjaxResult benefitSumDetail() {
        return AjaxResult.success(reportService.benefitSumDetail());
    }

    @GetMapping("/app/consumption/detail")
    public AjaxResult consumptionSumDetail() {
        return AjaxResult.success(reportService.consumptionSumDetail());
    }

    @GetMapping("/app/sales/item")
    public AjaxResult salesItem() {
        return AjaxResult.success(reportService.salesItemSumDetail());
    }
}
